/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_host_callback_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleHostCallbackProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("InnerTransact In");
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("get remote failed, error code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("send request failed, ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

void SleHostCallbackProxy::OnSleStateChange(int32_t state)
{
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleHostCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInt32(state), "write sle state failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleHostCallbackInterfaceCode::SLE_CALLBACK_STATE_CHANGE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleHostCallbackProxy::OnFlowMonitorEvent(float flow)
{
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleHostCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteFloat(flow), "write flow failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleHostCallbackInterfaceCode::SLE_CALLBACK_FLOW_MONITOR_EVENT, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}
} // SleStandard
} // NearLink
} // OHOS